/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sgep.services;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.Vector;
import sgep.params.Params;
import sgep.po.Application;

/**
 *
 * @author mg
 */
public class ApplicationServiceImpl implements ApplicationService {

    public List<Application> getAppList() throws Exception {
        List<Application> apps = new ArrayList<Application>();
        StringTokenizer st;
        String line, app;
        try {
            BufferedReader list =
                    new BufferedReader(new FileReader(Params.APP_HOME_DIR + Params.SUNTCP_LIST));
            while ((line = list.readLine()) != null) {
                st = new StringTokenizer(line, "\t");
                app = st.nextToken();
                Application a = new Application();
                a.setId(app);
                apps.add(a);
            }
        } catch (FileNotFoundException e) {
            System.out.println(Params.APP_HOME_DIR + Params.SUNTCP_LIST);
            System.out.println("There are no applications available.");
        }
        return apps;
    }

    public Application getApplication(String id) {
        Application appResult = new Application();
        StringTokenizer st;
        String line;
        String directory = Params.APP_HOME_DIR + id;

        try {
            BufferedReader read =
                    new BufferedReader(new FileReader(new File(directory, Params.SUNTCP_APP)));

            String app = read.readLine();
            String binary = read.readLine();
            String parallel = read.readLine();
            String xApp = read.readLine();
            String namef = read.readLine();
            String users = read.readLine();
            List export = new ArrayList();
            while ((line = read.readLine()) != null) {
                export.add(line);
            }
            appResult.setApp(app);
            appResult.setBinary(binary);
            appResult.setDirectory(directory);
            appResult.setExport(export);
            appResult.setId(id);
            appResult.setParallel(parallel);
            appResult.setNamef(namef);
            appResult.setUsers(users);
            appResult.setXApp(xApp);
            read.close();
            
        }catch (Exception e) {
            e.printStackTrace();
        }
        return appResult;
    }
}
